package com.huixuebao.insure.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.huixuebao.insure.dto.MerchantDTO;
import com.huixuebao.insure.entity.MerchantBasicsEntity;
import com.huixuebao.insure.mapper.MerchantBasicsMapper;
import com.huixuebao.insure.service.MerchantBasicsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author yufatao
 * @version 1.0
 * @date 2020-11-01 16:40
 */
@Service
public class MerchantBasicsServiceImpl implements MerchantBasicsService {

    @Autowired
    MerchantBasicsMapper merchantBasicsMapper;

    @Override
    public List<MerchantDTO> listMerchants() {
        List<MerchantBasicsEntity> list = merchantBasicsMapper.selectList(new LambdaQueryWrapper<MerchantBasicsEntity>().ne(MerchantBasicsEntity::getState,"0"));
        List<MerchantDTO> result = new ArrayList<>();
        list.stream().forEach(entity -> {
            MerchantDTO dto = new MerchantDTO();
            BeanUtil.copyProperties(entity, dto);
            result.add(dto);
        });
        return result;
    }

    @Override
    public List<MerchantBasicsEntity> gitMerchant(){

        List<MerchantBasicsEntity> list = merchantBasicsMapper.selectList(null);
        List<MerchantBasicsEntity> result = new ArrayList<>();

        return result;
    }

    @Override
    public List<MerchantBasicsEntity> getListByStatus() {
        List<MerchantBasicsEntity> list = merchantBasicsMapper.selectList(new LambdaQueryWrapper<MerchantBasicsEntity>()
                .eq(MerchantBasicsEntity::getSqlState, "0"));
        if(list!=null&&!list.isEmpty()){
            merchantBasicsMapper.update(null
                    ,new LambdaUpdateWrapper<MerchantBasicsEntity>().eq(MerchantBasicsEntity::getSqlState,"0")
                            .set(MerchantBasicsEntity::getSqlState,"1"));
        }
        return list;
    }

    @Override
    public List<MerchantBasicsEntity> getMerchantsDetails(String id) {
        List<MerchantBasicsEntity> list = merchantBasicsMapper.selectList(new LambdaQueryWrapper<MerchantBasicsEntity>()
        .eq(MerchantBasicsEntity::getId,id));
        return list;
    }
}